home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Environments
/
CGIshell 1.3.2
/
Pocket 6.5
/
Extensions
/
DataFiles.pf
next >
Wrap
Text File
|
1995-11-11
|
5KB
|
135 lines
( dataFiles 16 November 1995 - a possible scheme for using data files )
\ ======================= DataFiles =============================
\ Because they are available on all Macs and somewhat easier to implement,
\ File Control Blocks are used instead of the more modern fss records.
\ An FCB has the following format:
\ offset name size description
\ 12 ioCompletion 4 routine address
\ 16 ioResults 2 result (error) code
\ 18 ioNamePtr 4 pathname address - STRING[255]
\ 22 ioVRefNum 2 volume ref. number
\ 24 ioRefNum 2 file ref. num
\ 26 ioVersNum 1 ?
\ 27 ioPermssn 1 write permission - set for read only
\ 28 ioMisc 4 end of file & ?
\ 32 ioBuffer 4 address of read/write buffer
\ 36 ioReqCount 4 bytes to read/write
\ 40 ioActCount 4 actual bytes read/written
\ 44 ioPosMode 2 read/write positionning mode
\ 46 ioPosOffset 4 offset into the file
\ plus more stuff that isn't shown here. See Inside Mac
\ File errors:
\ -35 no such volume
\ -36 i/o error
\ -37 bad filename
\ -38 file not open
\ -39 logical eof reached
\ -40 mark position error
\ -42 too many files open
\ -43 file not found
\ -44 hardware volume lock
\ -45 file locked
\ -46 volume locked check box checked
\ -48 duplicate filename
\ -49 file already open
\ -50 negative count requested
\ -51 bad file ref. no.
\ -61 no permission
\ create space for the fcb and a word to access it
variable FCB 78 allot ( our File's Control Block )
: +FCB ( offset -- addr ) fcb + ; ( offset into fcb )
: FTRAP ( -- ) fcb >abs ,$ 205E ; ( movea.l [ps]+,a0 )
: CLOSE ( -- ) ftrap ,$ A001 ftrap ,$ A013 ; ( _Close & _FlushBuffer )
\ error handling
: .ERROR ( error# -- ) ." Disk Error" . close abort ;
variable ^ERROR ' .error ^error !
: DERROR ( -- error# ) 16 +fcb @ ;
: ?DERROR ( -- ) derror ?dup IF ^error @ execute THEN ;
\ set file descriptors
: !FILE ( string[255] wd# - )
fcb 80 0 fill \ clear the fc buffer
22 +fcb ! \ set vRefnum to n (zero means same folder)
>abs 18 +fcb 2! ; \ set name to string
\ open a file
: OPENFILE ( string[255] wd# - ) !file ftrap ,$ A000 ?derror ; \ _HOpen
: OPEN ( string[255] -- ) 0 openfile ; \ for the lazy: file in same folder
\ check a file
: ?FILEOK ( string[255] wd# -- flag ) \ true if exists, has permission, etc.
!file ftrap ,$ A000 derror 0= close ;
\ create a file
: NEWFILE ( string[255] wd# -- )
!file ftrap ,$ A008 ?derror ( _Create )
,s TEXT 32 +fcb 2! \ TEXT type
ftrap ,$ A00D ?derror ; ( _SetFileInfo )
\ delete a file
: DELFILE ( string[255] wd# -- )
!file ftrap ,$ A009 ?derror ; ( _Delete )
\ return the filesize !!! MUST BE <32K !!!
: @SIZE ( -- bytes ) ftrap ,$ A011 30 +fcb @ ; ( _GetEOF )
\ set some fcb parameters
: !SIZE ( bytes -- ) 38 +fcb ! ; \ set bytes-to-read/write
: !BUFF ( addr -- ) >abs 32 +fcb 2! ; \ set read/write buffer pointer
\ read/write with buffer addr and bytes to read/write on the stack
: READ ( addr count -- ) !size !buff ftrap ,$ A002 ?derror ; ( _Read )
: WRITE ( addr count -- ) !size !buff ftrap ,$ A003 ?derror ; ( _Write )
\ read until character, c, is encountered
: CREAD ( addr c -- bytes_read ) \ See Inside Mac for how this works
44 +fcb c! 128 45 +fcb c! \ setup ioPosMode
@size read 42 +fcb @ ; \ put lowbyte of ioActCount on stack
\ read/write file a byte at a time to/from the stack
: GETCHR ( -- c ) here 1 read here c@ ;
: PUTCHR ( c -- ) here c! here 1 write ;
\ -------------------Standard Reply Dialogs-------------------
variable SFREPLY 72 allot
: SFDATA ( -- filename wd# true OR false )
sfreply @ IF sfreply 10 + sfreply 6 + @ -1 ELSE 0 THEN ;
: SFPUTFILE ( defaultname prompt -- filename wd# -1 OR zero )
sfreply 74 0 fill
55 75 2>r \ top left corner
a>r a>r \ prompt.string default.filename.string
0 0 2>r \ dialog hook pointer
sfreply a>r \ reply record address
1 >r ,$ a9ea \ _SFPutFile
sfdata ;
: SFGETFILE ( -- filename.string[255] wd# -1 OR zero )
sfreply 74 0 fill
55 75 2>r \ top left corner
0 0 2>r \ unused
0 0 2>r \ filter procPtr
1 >r \ one type in the type list
,s TEXT SP@ 2>r \ keep the type list on the stack
0 0 2>r \ dialog hook pointer
sfreply a>r \ reply record
2 >r ,$ A9EA \ _SFGetFile
2drop \ get rid of the stacked type list
sfdata ;
\ --------------------LIST - a quick demo --------------------
: LIST ( -- )
sfgetfile IF openfile
@size 0 DO getchr emit LOOP
close THEN ;